package com.example.springbootvuetest.Utils;

import com.example.springbootvuetest.entity.Order;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 订单
 * @Author shuoye
 * @Date 2023/3/19
 * @Version 1.0
 */
public class OrderTask implements Delayed {
    /**
     * 延迟时间
     */
    @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    private long time;

    private Order order;

    public OrderTask() {

    }

    /**
     * @param order
     * @param time  订单超时时间
     */

    public OrderTask(Order order, long time) {
        this.order = order;
        this.time = System.currentTimeMillis() + 1000 * time;
    }

    /**
     * @param unit
     * @return 获取剩余到期时间, 剩余到期时间 = 到期时间 - 当前系统时间
     */
    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    /**
     * @param o
     * @return 按照剩余时间进行排序
     */
    @Override
    public int compareTo(Delayed o) {
        OrderTask Order = (OrderTask) o;
        return this.time - Order.time > 0 ? 1 : -1;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("{");
        sb.append("\"time\":")
                .append(time);
        sb.append(",\"order\":")
                .append(order);
        sb.append('}');
        return sb.toString();
    }
}
